var http = require('http');
var request = require('request');
var server = http.createServer((req, res) => {
    var targtUrl = req.url
    if (targtUrl.indexOf('type=') != -1) {
        console.log(123,req.url);
        let data = '';
        req.on('data', function (chunk) {
            console.log('接收到的客户端请求数据：' + chunk);
            data += chunk;
        })
        req.on('end', function(){
            var urlType = targtUrl.split('type=')[1];
            var paraObj = {};
            var paraArr = data.split("&");
            paraArr.forEach((item,index) => {
                var key = item.split("=")[0];
                var value = item.split("=")[1];
                paraObj[key] = value;
            })
            res.writeHead(
                200,
                {
                    "Access-Control-Allow-Origin": '*'
                }
            )
            if (urlType == 'verify') {
                request.post({
                    url: "./i/common/sms/getcode", formData: { ...paraObj }
                }, function (error, response, body) {
                    if (response.statusCode == 200) {
                        res.end(body);
                        console.log('body: ', body)
                    } else {
                        console.log('error: ', error);
                        res.end("Error: ", body);
                    }
                });
            }
        })
    } else {
        consoel.log('error');
    }
    
})
server.listen(3002);
console.log('server listening on port:3002');